package de.homelabs.bookmarkmanager.persistence.domain;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.joda.time.DateTime;

import de.homelabs.bookmarkmanager.domain.BookmarkType;

@Entity
@Table(name="bookmarks")
public class HBookmark {

	//vars
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;														//unique id
	private String title;												//title of bookmark/folder
	private URL link;													//url (HTTP) of the bookmark, empty of folder
	private String tags;												//list of tags, empty if folder
	private BookmarkType type;												//default type
	private DateTime createdAt;
	
	//Tree mapping
	@ManyToOne
	@JoinColumn(name="parent_id")
	private HBookmark parent;
	@OneToMany(mappedBy="parent", cascade=CascadeType.ALL)
	private List<HBookmark> children = new ArrayList<HBookmark>();		//childreen
	
	/**
	 * constructor
	 */
	public HBookmark(){ init();}
	
	/**
	 * constructor
	 * 
	 * @param pTitle
	 * @param pLink
	 * @param pTags
	 * @param pChildreen
	 * @param pType
	 */
	public HBookmark(String pTitle, URL pLink, String pTags, List<HBookmark> pChildren, BookmarkType pType){
		init();
		this.title 		= pTitle;
		this.link 		= pLink;
		this.tags 		= pTags;
		this.children 	= pChildren;
		this.type 		= pType; 
	}

	/*******************
	 * getter / setter *
	 *******************/
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public URL getLink() {
		return link;
	}

	public void setLink(URL link) {
		this.link = link;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

	public List<HBookmark> getChildren() {
		return children;
	}

	public void setChildren(List<HBookmark> children) {
		this.children = children;
	}

	public BookmarkType getType() {
		return type;
	}

	public void setType(BookmarkType type) {
		this.type = type;
	}
	
	public void addChild(HBookmark child){
		this.children.add(child);
	}

	public DateTime getCreatedAt() {
		return createdAt;
	}
	
	public HBookmark getParent() {
		return parent;
	}

	public void setParent(HBookmark parent) {
		this.parent = parent;
	}
	
	public HBookmark getChildAt(int index){
		return this.children.get(index);
	}
	/**
	 * init default parameter
	 */
	private void init(){
		createdAt = new DateTime();
	}
}
